package Reflect;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Scanner;

public class ReflectDemo4 {
    public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        Person p = new Person();
        p.sayHello();

        Scanner scanner1 = new Scanner(System.in);
        System.out.println("请输入类名");
        String className = scanner1.nextLine();
        Class cls = Class.forName(className);
        Object obj = cls.newInstance();

        Scanner scanner2 = new Scanner(System.in);
        System.out.println("请输入方法名");
        String methodName = scanner2.nextLine();
        Method method = cls.getMethod(methodName);
        method.invoke(obj);
    }
}
